@extends('layouts.'.getTheme())
@section('title','教学获奖成果上传')
@section('css')
<link href="{{asset(getThemeAssets('css/toastr.min.css'))}}" rel="stylesheet">
<style type="text/css">
    .widget .row span{
      font-size: 15px;
      font-weight: bold;
    }
    .p-md{
        padding-top: 6px;
        padding-bottom: 0px;
        margin-top: 0px;
        margin-bottom: 30px;
    }
    .m-b-md{
        padding-top: 3px;
    }
    .page-heading{
        padding-bottom: 2px;
    }
    .alert{
        /*padding-bottom: 0px;*/
        padding-left: 23px;
        padding-right: 23px;
        margin-bottom: 0px;
    }
    .alert p{
        font-size: 14px;
        font-weight: 12px;
    }
    .alert a{
        text-decoration: underline;
    }
    .canceled{
        margin: 0;
        padding: 0;
    }
    #login-form {
        margin-top: 30px;
    }
    #login-form img{
        width: 100%;
        margin-left: 0;
    }
    #footerrow {
      margin-top: 100px;
    }
    .col-sm-6 {
      padding: 3px;
    }
</style>
@endsection
@section('content')

<div class="row wrapper border-bottom page-heading">
  <div class="col-sm-10">
    <h2>{!!trans('upload.title')!!}</h2>
    <ol class="breadcrumb">
        <li>
            <a href="{{url('admin')}}">{!!trans('home.title')!!}</a>
        </li>
        <li class="active">
            <strong>{!!trans('upload.title')!!}</strong>
        </li>
    </ol>
  </div>
  <div class="col-sm-2">
    <div class="title-action">
      <a class="btn btn-primary btn-block" href="{{ url('/admin/upload/uploadexcel') }}"><i class="fa fa-reply"></i>切换Excel方式上传</a>
    </div>
  </div>
</div>
<div id="login-form">
    <div class="logo">
        <img id="logo" src="{{asset(getThemeAssets('img/logo2.png'))}}">
    </div>
    <div class="row">
      <form class="form-horizontal" id="awardinfo" enctype="multipart/form-data" method="" action="">
      {{ csrf_field() }}
        <div class="col-lg-offset-3 col-lg-6">
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-user"></span> -->
              <label>教师姓名：</label>
            </div>
            <input type="text" class="form-control" id="username" name="username"
                    placeholder="{{ auth()->user()->name}}" disabled="true" required>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-user"></span> -->
              <label>职工编号：</label>

            </div>
            <input type="text" class="form-control" id="username" name="username"
                    placeholder="{{ auth()->user()->jobnumber}}" disabled="true" required>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-star"></span> -->
              <label>奖项内容：</label>
            </div>
            <input type="text" class="form-control" id="award_content" name="award_content" placeholder="具体奖项名称" required>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-star"></span> -->
              <label>奖项分类：</label>

            </div>
            <select class="form-control" id="award_category" name="award_category">
                <option class="form-control" value ="教学成果奖">教学成果奖</option>
                <option class="form-control" value ="教育科学研究优秀成果奖">教育科学研究优秀成果奖</option>
                <option class="form-control" value="讲课比赛">讲课比赛</option>
                <option class="form-control" value="微课比赛">微课比赛</option>
                <option class="form-control" value="课程">课程</option>
                <option class="form-control" value="教材">教材</option>
                <option class="form-control" value="专业负责人业绩津贴">专业负责人业绩津贴</option>
              </select>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-time"></span> -->
              <label>获奖日期：</label>

            </div>
            <input type="date" class="form-control" id="award_time" name="award_time" placeholder="具体获奖时间,格式：2018-01-01" required>
          </div>
        </div>

        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-star"></span> -->
              <label>奖项级别：</label>

            </div>
            <select class="form-control" id="award_level" name="award_level">
                <option class="form-control" value ="苏理工">苏理工</option>
                <option class="form-control" value ="江科大">江科大</option>
                <option class="form-control" value="市级">市级</option>
                <option class="form-control" value="省级">省级</option>
                <option class="form-control" value="国家级">国家级</option>
                <option class="form-control" value="无">无</option>
              </select>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-star"></span> -->
              <label>具体奖项：</label>

            </div>
            <select class="form-control" id="award_prize" name="award_prize">
                <option class="form-control" value="特等奖">特等奖</option>
                <option class="form-control" value="一等奖">一等奖</option>
                <option class="form-control" value="二等奖">二等奖</option>
                <option class="form-control" value="三等奖">三等奖</option>
                <option class="form-control" value="四等奖">四等奖</option>
                <option class="form-control" value="优胜奖">优胜奖</option>
                <option class="form-control" value="优秀奖">优秀奖</option>
                <option class="form-control" value="优秀组织奖">优秀组织奖</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-time"></span> -->
              <label class="input-group">奖金：</label>

            </div>
            <input type="text" class="form-control" id="award_money_amount" name="award_money_amount" placeholder="单位为元" required>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-time"></span> -->
              <label class="input-group">奖金计算年度：</label>

            </div>
            <input type="text" class="form-control" id="award_calc_year" name="award_calc_year" placeholder="不清楚可不用填写" required>
          </div>
        </div>
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-time"></span> -->
              <label>其他成员：</label>

            </div>
            <input type="text" class="form-control" id="allusers" name="allusers" placeholder="成员以逗号分隔" required>
          </div>
        </div>   
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-addon">
              <!-- <span class="glyphicon glyphicon-tag"></span> -->
              <label>扫描文件：</label>

            </div>
            <input type="file" class="form-control" accept="image/jpg,image/jpeg,image/png" id="uploadimg" name="awardimg" multiple />
            <div id="preview">
              
            </div>
          </div>
        </div>     
        </div>
        
        <div class="form-group">
          <div class="col-sm-12 ">
            
            <div class="col-sm-offset-3 col-sm-3">
                <button type="button" id="submitbtn" class="btn btn-primary btn-block">确认提交获奖记录</button>
            </div>
            <div class="col-sm-3">
                <a href="{{ url('/admin/upload/uploadexcel') }}">
                <button type="button" class="btn btn-primary btn-block">切换为Excel文件上传</button>
                </a>
            </div>
          </div>
        </div>
      </form>
  </div>
</div>
                        

@endsection
@section('js')
<script src="{{asset(getThemeAssets('js/toastr.min.js'))}}"></script>
<script type="text/javascript">
    $(document).ready(function()  {
        setTimeout(function() {
                      toastr.options = {
                          closeButton: true,
                          progressBar: true,
                          showMethod: 'slideDown',
                          timeOut: 4000
                          };
                      toastr.error('注意：本页面适用于手动录入单条获奖记录，批量录入请选择Excel方式！');
        }, 100);
        $("#uploadimg").change(function() {
            var file;
            var destination;
            destination = $("#preview");
            destination.innerHTML = ''; // 预览区置空
            // 循环遍历用户选择的文件
            for (var x=0,xlen = this.files.length;x<xlen;x++) {
                file = this.files[x];
                // 对上传文件大小做限制 200kb
                if (file.size > 204800) {
                  swal("请上传图片大小不要超过200KB！","","warning");
                  $("#uploadimg").val("");
                  $("#preview").html("");
                  return;
                }
                if (file.type.indexOf('image') != -1) { // 判断是否为图片
                    var reader = new FileReader(); // 预览对象
                    reader.readAsDataURL(file);
                    reader.onload = function(e) {
                        var img = new Image();
                        img.src = e.target.result;
                        destination.append(img);
                    }
                }
            }
        });

      // 上传事件监听
      $("#submitbtn").click(function() {
          var awardInfo = [];
          awardInfo['award_content'] = $("#award_content").val();
          awardInfo['award_category'] = $("#award_category").val();
          awardInfo['award_time'] = $("#award_time").val();
          awardInfo['award_level'] = $("#award_level").val();
          awardInfo['award_prize'] = $("#award_prize").val();
          awardInfo['award_money_amount'] = $("#award_money_amount").val();
          awardInfo['award_calc_year'] = $("#award_calc_year").val();
          awardInfo['allusers'] = $("#allusers").val();
          if (awardInfo['award_content'] == '') {
              swal("获奖内容不能为空！","","warning");
              return;
          }
          if (awardInfo['award_time'] == '') {
              swal("获奖时间不能为空！","","warning");
              return;
          }else {
              var time_vali = /^[0-9]{4}(-)[0-9]{2}(-)[0-9]{2}$/;
              if (!time_vali.exec(awardInfo['award_time'])) {
                  swal("请检查获奖时间填写格式！","","warning");
                  return;   
              }
          }
          // 对不上传图片文件进行用户提示
          if ($("#uploadimg").val() == "") {
              swal("对不起！请至少上传一张小于200KB的图片证明文件。","","warning");
              $("#preview").html("");
              return;
          }
          var allFiles = document.querySelector("#uploadimg").files;
          // 实例化表单数据对象
          var formData = new FormData();
          // 遍历图片文件列表并插入至表单中
          for (var i=0,file;file = allFiles[i];i++) {
              // 包含文件名称和文件对象
              formData.append(file.name,file);
          }
          console.log(awardInfo)
          formData.append('award_content',awardInfo['award_content']);
          formData.append('award_category',awardInfo['award_category']);
          formData.append('award_level',awardInfo['award_level']);
          formData.append('award_prize',awardInfo['award_prize']);
          formData.append('award_money_amount',awardInfo['award_money_amount']);
          formData.append('award_calc_year',awardInfo['award_calc_year']);
          formData.append('allusers',awardInfo['allusers']);
          formData.append('award_time',awardInfo['award_time']);
          console.log(formData);
          // ajax请求，向数据库进行信息提交
          $.ajaxSetup ({
                     headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
            });

          $.ajax({
              url: "{{ url('/admin/upload/uploadaward')}}",
              type: 'post',
              data: formData,
              processData: false,
              contentType: false, 
              success: function(res) {
                  if (res['res'] == 1) {
                      swal("上传获奖记录成功！请到个人档案进行核对！","","success");
                      $("#award_content").val("");
                      $("#award_money_amount").val("");
                      $("#award_calc_year").val("");
                      $("#allusers").val("");
                      $("#award_time").val("");
                      $("#uploadimg").val("");
                      $("#preview").html('');
                  }else {
                      swal("上传失败！请联系管理员！","","warning");
                  }
              }
          });

      });
    });
</script>
@endsection